<template>
  <div class="app-container">
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="5">
            <el-form-item label="操作模块">
              <el-input style="width: 55%" v-model="searchQuery.title" placeholder="操作模块"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="5">
            <el-form-item label="操作人员">
              <el-input style="width: 55%" v-model="searchQuery.operName" placeholder="操作人员"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="操作时间">
              <el-date-picker v-model="createTimes" type="datetimerange" range-separator="至" start-placeholder="开始时间"
                              end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss"
                              style="margin-right: 10px;width: 100%;"
              />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()">搜 索</el-button>
              <el-button type="info" icon="el-icon-refresh" size="mini" @click="resetData">重 置</el-button>
            </el-form-item>

          </el-col>
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <!--    <div class="tools-div">
        </div>-->

    <!-- 列表 -->
    <el-table v-loading="listLoading" :data="list" stripe border style="width: 100%;margin-top: 10px;">

      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="title" label="操作模块"/>
      <el-table-column prop="businessType" label="操作类型"/>
      <el-table-column prop="requestMethod" label="请求方式"/>
      <el-table-column prop="operName" label="操作人员"/>
      <el-table-column prop="operIp" label="操作IP"/>
      <el-table-column prop="method" label="请求方法" width="200"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <div v-if="scope.row.status === 1" style="color: green">成功</div>
          <div v-else style="color: red">失败</div>
        </template>
      </el-table-column>
      <el-table-column prop="operTime" label="访问时间"/>
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="edit(scope.row.id)" title="详情">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination :current-page="page" :total="total" :page-size="pageSize"
                   style="padding: 30px 0; text-align: center;"
                   layout="total, prev, pager, next, jumper" @current-change="fetchData"
    />

    <!-- 详情列表 -->
    <el-dialog title="详情显示" :visible.sync="dialogVisible" width="40%">
      <el-descriptions class="margin-top" :column="1" border>
        <el-descriptions-item>
          <template slot="label">
            模块标题
          </template>
          {{ sysOperLog.title }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            操作类型
          </template>
          {{ sysOperLog.businessType }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            方法名称
          </template>
          {{ sysOperLog.method }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            请求方式
          </template>
          {{ sysOperLog.requestMethod }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            操作类别
          </template>
          {{ sysOperLog.operatorType }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            操作人员
          </template>
          {{ sysOperLog.operName }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            请求地址
          </template>
          {{ sysOperLog.operUrl }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            请求主机
          </template>
          {{ sysOperLog.operIp }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            请求参数
          </template>
          {{ sysOperLog.operParam }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            返回参数
          </template>
          {{ sysOperLog.jsonResult ? sysOperLog.jsonResult : '无' }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            模块标题
          </template>
          {{ sysOperLog.title }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            操作状态
          </template>
          {{ sysOperLog.status === 0 ? '失败' : '成功' }}
        </el-descriptions-item>

        <el-descriptions-item v-if="sysOperLog.errorMsg!=null || sysOperLog.errorMsg!=''">
          <template slot="label">
            错误消息
          </template>
          {{ sysOperLog.errorMsg }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            操作时间
          </template>
          {{ sysOperLog.operTime }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api/system/operLog'

export default {
  data() {
    return {
      listLoading: false, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      pageSize: 6, // 每页记录数
      searchQuery: {}, // 查询表单对象

      createTimes: [],
      dialogVisible: false,
      sysOperLog: {}
    }
  },
  created() {
    // 调用列表方法
    this.fetchData()
  },

  methods: {
    // 列表
    fetchData(pageNum = 1) {
      this.page = pageNum
      if (this.createTimes && this.createTimes.length == 2) {
        this.searchQuery.createTimeBegin = this.createTimes[0]
        this.searchQuery.createTimeEnd = this.createTimes[1]
      }

      api.getPageList(this.page, this.pageSize, this.searchQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
      })
    },

    // 重置
    resetData() {
      // 清空表单
      this.searchQuery = {}
      // 查询所有数据
      this.fetchData()
    },

    // 详情
    edit(id) {
      this.dialogVisible = true

      api.getDetails(id).then(response => {
        this.sysOperLog = response.data
      })
    }
  }
}
</script>
